package com.example.zhu.service.Impl;

import com.example.zhu.dto.ManageDTO;
import com.example.zhu.entity.Manage;
import com.example.zhu.mapper.ManageBookMapper;
import com.example.zhu.mapper.ManageMapper;
import com.example.zhu.result.Result;
import com.example.zhu.service.ManageService;
import com.example.zhu.vo.ManageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ManageMapper manageMapper;

    @Autowired
    private ManageBookMapper manageBookMapper;

    /**
     * 新增一个记录
     * @param manageDTO
     * @return
     */
    @Transactional
    public Result insert(ManageDTO manageDTO) {

        /**
         * 先判断takeKey 和 sendKey是否不存在
         */
        String take = manageDTO.getTakeKey();
        String send = manageDTO.getSendKey();
        List<Manage> manageVOS = manageMapper.selectAll();
        for (Manage manageVO : manageVOS) {
            String takeKey = manageVO.getTakeKey();
            String sendKey = manageVO.getSendKey();
            if(takeKey.equals(take) || sendKey.equals(send)){
                return Result.error("号码已经存在");
            }
        }
        manageMapper.insert(manageDTO);
        List<String> areas = manageDTO.getArea();
        if(areas!=null){
            manageBookMapper.insert(areas,manageDTO.getId());
        }
        return Result.success();
    }

    /**
     * 更新一条记录
     * @param manageDTO
     * @return
     */
    @Transactional
    public Result update(ManageDTO manageDTO) {
        manageMapper.update(manageDTO);
        manageBookMapper.deleteByManage(manageDTO.getId());
        List<String> areas = manageDTO.getArea();
        if(areas!=null&&areas.size()!=0){
            manageBookMapper.insert(areas,manageDTO.getId());
        }
        return Result.success();
    }

    /**
     * 查询编辑信息
     * @param id
     * @return
     */
    public Manage selectEditInfo(Integer id) {
        Manage manageVO = manageMapper.selectEditInfo(id);
        Integer id_ = manageVO.getId();
        List<String> areas = manageBookMapper.selectInfo(id_);
        manageVO.setArea(areas);
        return manageVO;
    }

    /**
     * 查询所有划分信息
     *
     * @return
     */
    public List<ManageVO> selectAll() {
        List<Manage> list = manageMapper.selectAll();
        List<ManageVO> res = new ArrayList<>();
        for (Manage manage : list) {
            String takeKey = manage.getTakeKey();
            String sendKey = manage.getSendKey();
            Integer id = manage.getId();
            List<String> areas = manageBookMapper.selectInfo(id);
            ManageVO manage_ = ManageVO.builder()
                    .id(id)
                    .takeKey(takeKey)
                    .sendKey(sendKey)
                    .area(areas)
                    .build();
            res.add(manage_);
        }
        return res;
    }


}
